<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>推荐服务 - 智能农业推荐系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        .recommend-section {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 60px 0;
        }
        .form-card {
            background: white;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        .btn-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border: none;
            color: white;
        }
        .btn-gradient:hover {
            background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%);
            color: white;
        }
        .feature-icon {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            margin: 0 auto 20px;
        }
        .loading-spinner {
            text-align: center;
            margin: 20px 0;
        }
    </style>
    <link rel="stylesheet" href="http://localhost:63342/css/recommend.css">
</head>
<body>
<!-- 导航栏 -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
    <div class="container">
        <a class="navbar-brand" href="/">
            <i class="fas fa-seedling me-2"></i>智能app推荐系统
        </a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav ms-auto">
                <li class="nav-item">
                    <a class="nav-link" href="/">首页</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link active" href="/recommend">推荐服务</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/api/test" target="_blank">服务测试</a>
                </li>
            </ul>
        </div>
    </div>
</nav>

<!-- 推荐服务区域 -->
<section class="recommend-section">
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-lg-8">
                <div class="text-center mb-5">
                    <h1 class="display-5 mb-3">
                        <i class="fas fa-magic me-3"></i>智能推荐服务
                    </h1>
                    <p class="lead">输入用户ID，获取个性化的app应用推荐</p>
                </div>

                <div class="form-card p-5">
                    <form id="recommendForm">
                        <div class="row g-3">
                            <div class="col-md-8">
                                <label for="userId" class="form-label">用户ID</label>
                                <input type="text" class="form-control form-control-lg" id="userId"
                                       name="uid" placeholder="请输入用户ID（例如：device_id1)" required>
                            </div>
                            <div class="col-md-4">
                                <label class="form-label">&nbsp;</label>
                                <button type="submit" class="btn btn-gradient btn-lg w-100">
                                    <i class="fas fa-search me-2"></i>获取推荐
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</section>

<!-- 推荐结果区域 -->
<section class="py-5">
    <div class="container">
        <div id="resultArea" style="display: none;">
            <h2 class="text-center mb-4">推荐结果</h2>
            <div class="row">
                <div class="col-lg-8 mx-auto">
                    <div class="card">
                        <div class="card-header">
                            <h5 class="mb-0">
                                <i class="fas fa-user me-2"></i>
                                用户ID: <span id="resultUserId"></span>
                            </h5>
                        </div>
                        <div class="card-body" id="recommendationContainer">
                            <div class="loading-spinner">
                                <i class="fas fa-spinner fa-spin fa-2x"></i>
                                <p>正在获取推荐...</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>


<!-- 功能说明 -->
<section class="py-5">
    <div class="container">
        <h2 class="text-center mb-5">推荐算法说明</h2>
        <div class="row">
            <div class="col-md-4 mb-4">
                <div class="text-center">
                    <div class="feature-icon">
                        <i class="fas fa-chart-line fa-2x"></i>
                    </div>
                    <h5>协同过滤</h5>
                    <p>基于用户行为相似性，为用户推荐相似用户喜欢的应用。</p>
                </div>
            </div>
            <div class="col-md-4 mb-4">
                <div class="text-center">
                    <div class="feature-icon">
                        <i class="fas fa-tags fa-2x"></i>
                    </div>
                    <h5>内容推荐</h5>
                    <p>根据应用的特征和用户的历史偏好进行匹配推荐。</p>
                </div>
            </div>
            <div class="col-md-4 mb-4">
                <div class="text-center">
                    <div class="feature-icon">
                        <i class="fas fa-brain fa-2x"></i>
                    </div>
                    <h5>机器学习</h5>
                    <p>使用Spark MLlib实现的高级推荐算法，提供精准推荐。</p>
                </div>
            </div>
        </div>
    </div>
</section>

<!-- 页脚 -->
<footer class="bg-dark text-white py-4">
    <div class="container text-center">
        <p>&copy; 2025 智能app推荐系统 数科22-4第7小组</p>
    </div>
</footer>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>

<!-- 环境变量配置（可根据环境修改BASE_URL） -->
<script>
    window.API_CONFIG = {
        //BASE_URL: 'https://api.smart-agriculture.com/v1' // 修改为实际API域名
        // 开发环境可改为：
        BASE_URL: 'http://localhost:8080/api'
    };
</script>

<script>
    // 单用户推荐
    document.getElementById('recommendForm').addEventListener('submit', function(e) {
        e.preventDefault();
        const userId = document.getElementById('userId').value.trim();
        if (!userId) {
            showAlert('请输入用户ID', 'danger');
            return;
        }

        // 显示结果区域和加载状态
        document.getElementById('resultArea').style.display = 'block';
        document.getElementById('resultUserId').textContent = userId;

        // 调用API（使用环境变量中的BASE_URL）
        fetch(`${window.API_CONFIG.BASE_URL}/get-recommendation?uid=${encodeURIComponent(userId)}`, {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
            }
        })
            .then(response => {
                if (!response.ok) {
                    throw new Error(`HTTP错误: ${response.status}`);
                }
                return response.json();
            })
            .then(data => {
                if (data.success) {
                    displayRecommendations(data.data);
                } else {
                    showAlert(data.message, 'danger');
                }
            })
            .catch(error => {
                showAlert(`请求失败: ${error.message}`, 'danger');
                console.error('请求错误:', error);
            });
    });



    // 显示推荐结果
    function displayRecommendations(recommendations) {
        const container = document.getElementById('recommendationContainer');
        if (recommendations && recommendations.length > 0) {
            let html = '<div class="row">';
            recommendations.forEach((item, index) => {
                html += `
                        <div class="col-md-6 mb-3">
                            <div class="card h-100">
                                <div class="card-body">
                                    <h6 class="card-title">
                                        <i class="fas fa-app-store-ios me-2 text-primary"></i>
                                        推荐应用 ${index + 1}
                                    </h6>
                                    <p class="card-text">${item}</p>
                                </div>
                            </div>
                        </div>
                    `;
            });
            html += '</div>';
            container.innerHTML = html;
        } else {
            container.innerHTML = '<div class="alert alert-info">暂无推荐结果</div>';
        }
    }


    // 显示提示信息
    function showAlert(message, type) {
        const container = document.getElementById('recommendationContainer');
        container.innerHTML = `<div class="alert alert-${type}">${message}</div>`;
    }
</script>
</body>
</html>